package com.yiwise.controller;

import com.yiwise.common.CommonErrorCode;
import com.yiwise.common.ResultModel;
import com.yiwise.service.PhoneConvertService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * 号码解密接口
 */
@RestController
@RequestMapping({ "/api/phone" })
public class DemoDecryptController {
    private static final Logger logger = LoggerFactory.getLogger(DemoDecryptController.class);

    @Resource(name = "simplePhoneConvertService")
    PhoneConvertService simplePhoneConvertService;

    @Resource(name = "aesConvertService")
    PhoneConvertService aesConvertService;

    @GetMapping({ "/getRealNumber" })
    public ResultModel getRealNumber(@RequestParam String encryptedPhone) {
        try {
            logger.info(encryptedPhone);
            String realPhone = simplePhoneConvertService.decrypt(encryptedPhone);
            // 也可以使用AES，实现二次加密等场景
            // String realPhone = aesConvertService.decrypt(encryptedPhone);
            return ResultModel.success(realPhone);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            return ResultModel.fail(CommonErrorCode.UNKNOWN_ERROR, e.getMessage());
        }
    }
}
